<?php
namespace Shopmobile\Controller;
use Think\Controller;
class UserController extends ExtendController {
	public function index(){
		$tida_mixnick = I("tida_mixnick");
		cookie("tida_mixnick",$tida_mixnick);
		$this->ajaxReturn();
	}
	public function appname(){
		$appname = I("appname");
		cookie("appname",$appname);
		$this->ajaxReturn();
	}
	public function address(){
		$ADR = D("MainUserAddress");
		$ADR->sid=$this->sid;
		$ADR->uid=$this->uid;
		$address = $ADR->getUserDefaultAddress();

		$this->assign("address",current($address));
		$this->display();
	}
	public function save_addr(){
		$name = I("name");
		$mobile = I("mobile");
		$picker = explode(" ",I("picker"));
		$address = I("address");
		
		$data["sid"]=$this->sid;
		$data["kxdr_uid"]=0;
		$data["shop_uid"]=$this->shop_uid;
		$data["sys_uid"]=$this->sys_uid;
		$data["nationality"]="中国";
		$data["province"]=$picker[0];
		$data["city"]=$picker[1];
		$data["city_part"]=$picker[2];
		$data["address"]=$address;
		$data["name"]=$name;
		$data["tel"]=$mobile;
		$data["is_def"]=true;
			
		$ADR = M("UserAddress");
		$adwh["sid"]=$this->sid;
		$adwh["shop_uid"]=$this->shop_uid;
		$addid = $ADR->where($adwh)->getField("id");
		if($addid){
			$data["id"]=$addid;
			$re = $ADR->save($data);
		}else{
			$re = $ADR->add($data);
		}
		
		$this->ajaxReturn(Array("hasError"=>!$re));
	}
	public function checkwl(){
		$mygid = I("mygid");
		$gid = I("gid");
		$getway = I("getway");
		
		// sm_gifts_sendlog
		
		$UG = M("UsersGifts");
		$ugw["id"]=$mygid;
		$ugw["sid"]=$this->sid;
		$ugw["giftid"]=$gid;
		$wlinfo = current($UG->where($ugw)->getField("id,wlid,wlcode"));

		if(!$wlinfo["wlid"] && !$wlinfo["wlcode"]){
			$ADD = M("UserAddress");
			$addwh["sid"]=$this->sid;
			$addwh["shop_uid"]=$this->shop_uid;
			$address = current($ADD->where($addwh)->getField("`id`,`nationality`,`province`,`city`,`city_part`,`address`,`name`,`tel`"));
			if(!$address){
				$code="need_address";
				$msg = "您还没有邮寄地址，请输入邮寄地址！";
			}else{
				$code="check_address";
				$msg = "奖品将会通过以下地址邮寄给您。";
				$reda["myadd"] = $address;
			}
			$ARE = D("Area");
			$area = $ARE->getArea();
		}else{
			$ADD = M("GiftsSendlog");
			$addwh["sid"]=$this->sid;
			$addwh["shop_uid"]=$this->shop_uid;
			$addwh["egid"]=$gid;
			$addwh["ugid"]=$mygid;
			$address = current($ADD->where($addwh)->getField("`id`,`nationality`,`province`,`city`,`city_part`,`address`,`true_name` AS `name`,`mobile` AS `tel`"));
			$code="gift_onroad";
			$msg = "奖品已经通过以下地址发送给您，请注意查收。";
			$reda["myadd"] = $address;
		}
		$WL = C("WL");
		$wlinfo["wlcode"] = $WL[$wlinfo["wlcode"]];
		$reda["wlinfo"]=$wlinfo;
		$reda["code"]=$code;
		$reda["msg"]=$msg;
		$reda["area"]=$area;
		$this->ajaxReturn($reda);
	}

	public function collect_item(){
		$item_iid = I("iid");
		$gid = I("gid");
		$COLL = M("UserGiftsItemcollect");
		$coll["sid"]=$this->sid;
		$coll["egid"]=$gid;
		$coll["user_id"]=$this->shop_uid;
		$coll["item_iid"]=$item_iid;
		$coll["is_collect"]=true;
		$coll["add_time"]=date("Y-m-d H:i:s");
		$re = $COLL->add($coll);
		$this->ajaxReturn(Array("hasError"=>!$re));
	}
	public function addcart_item(){
		$item_iid = I("iid");
		$gid = I("gid");
		$COLL = M("UserGiftsItemaddcart");
		$coll["sid"]=$this->sid;
		$coll["egid"]=$gid;
		$coll["user_id"]=$this->shop_uid;
		$coll["item_iid"]=$item_iid;
		$coll["is_addcart"]=true;
		$coll["add_time"]=date("Y-m-d H:i:s");
		$re = $COLL->add($coll);
		$this->ajaxReturn(Array("hasError"=>!$re));
	}
	public function share_done(){
		$item_iid = I("iid");
		$gid = I("gid");
		$mygid = I("mygid");
		$getway = I("getway");
		$show_title = I("show_title");
		$show_content = I("show_content");
		$way = I("way");
		$code = I("code");
		
		$COLL = M("UserGiftsFriendshow");
		$coll["sid"]=$this->sid;
		$coll["egid"]=$gid;
		$coll["user_id"]=$this->shop_uid;
		$coll["title"]=$show_title;
		$coll["content"]=$show_content;
		$coll["way"]=$way;
		$coll["code"]=true;
		$coll["add_time"]=date("Y-m-d H:i:s");
		$re = $COLL->add($coll);
		
		$this->ajaxReturn(Array("hasError"=>!$re));
	}
	public function mission(){
		$mis = I("mis");
		$eid = I("eid");
		$oid = I("oid");
		$do_jifen_exchange = I("do_jifen_exchange");
		$jifen = I("jifen");
		$jifen_gift = I("gift");
		$iid = I("iid");
		$isv_ext = I("isvExt");
		$curdate = I("curdate");
		$SHOP = D("Mission");
		$SHOP->mission=$mis;
		$SHOP->sid=$this->sid;
		$SHOP->eid=$eid;
		$SHOP->oid=$oid;
		$SHOP->shop_uid=$this->shop_uid;
		$SHOP->mixednick=$this->mixednick;
		$SHOP->do_jifen_exchange=$do_jifen_exchange;
		$SHOP->jifen=$jifen;
		$SHOP->jifen_gift=$jifen_gift;
		$SHOP->iid=$iid;
		$SHOP->isv_ext=$isv_ext;
		$SHOP->signCurrentDate=$curdate;
		$re = $SHOP->doMission();
		if($re){
			
			/*
			对老代码的兼容，主要出现在订单兑换过程
			$reda["status"]=false;
			$reda["score"]=0;
			$reda["giftname"]="抱歉，这个订单号已经被".substr($this->mixednick,0,1)."***兑换过了";
			*/
			
			$reda["hasError"] = $re["status"];
			$reda["msg"] = $re["giftname"];
		}else{
			$reda["hasError"]=$SHOP->hasError;
			$reda["msg"]=$SHOP->msg;
			if($mis=="jifen"){
				$reda["setting"]=$SHOP->jifen_setting;
			}
			if($mis=="cartadd"){
				$reda["cart_cfg"]=$SHOP->cart_cfg;
				$reda["setting"]=$SHOP->cart_items;
			}
			if($mis=="cartadd_check"){
				$reda["setting"]=$SHOP->code;
			}
			if($mis=="sign"){
				$reda["setting"]=$SHOP->signList;
			}
		}
		$reda["gift_info"]["jifen"]=$SHOP->get_jifen?$SHOP->get_jifen:0;
		$reda["gift_info"]["score"]=$SHOP->get_score?$SHOP->get_score:0;
		$this->ajaxReturn($reda);
	}
	public function sign_cur_day(){
		$curdate = I("curdate");
		$Seeds = D("Shopmodule/User");
		$Seeds->suid = $this->shop_uid;
		$Seeds->sid = $this->sid;
		$Seeds->signCurrentDate = $curdate;
		$info = $Seeds->qiandao();
		$reda["hasError"] = $info["status"];
		$reda["msg"] = $info["msg"];
		$this->ajaxReturn($reda);
	}
	public function sign_pass_day(){
		$curdate = I("curdate");
		$Seeds = D("Shopmodule/User");
		$Seeds->suid = $this->shop_uid;
		$Seeds->sid = $this->sid;
		$Seeds->buqianday = $curdate;
		$info = $Seeds->buqian();
		$reda["hasError"] = $info["status"];
		$reda["msg"] = $info["msg"];
		$this->ajaxReturn($reda);
	}
	public function sign_ling_jiang(){
		$gid = I("gid");
		$eid = I("eid");
		$curdate = I("date");
		
		$SHOP = D("Mission");
		$SHOP->mission="get_sign_gift";
		$SHOP->eid=$eid;
		$SHOP->sid=$this->sid;
		$SHOP->shop_uid=$this->shop_uid;
		$SHOP->mixednick=$this->mixednick;
		$SHOP->gid=$this->gid;
		$SHOP->signCurrentDate=$curdate;
		$SHOP->doMission();
		
		$reda["hasError"]=$SHOP->hasError;
		$reda["msg"]=$SHOP->msg;
		$this->ajaxReturn($reda);
	}
	public function collect_shop(){
		$eid = I("eid");
		if(C("TAE")){
			$cacheService->set("collect".$eid.$this->shop_uid,"1",C("CollectTimeOut"));
		}else{
			S("collect".$eid.$this->shop_uid,"1",C("CollectTimeOut")); //4小时过期，3600*4
		}
		// 数据库是永久的，应当使用24小时过期的缓存存储收藏状态
		// $EVU = M("EventsUsers");
		// $re = $EVU->where(Array("eventid"=>$eid,"uzuid"=>$this->shop_uid))->save(Array("collect"=>1));
		$this->ajaxReturn(Array("hasError"=>$re));
	}
	
	public function pc_collect(){
		$shop_uid = I("shop_uid");
		$sid = I("sid");
		$eid = I("eid");
		$EUser = M("EventsUsers");
		$data["collect"]=1;
		$re["status"]=$EUser->where(Array("uzuid"=>$shop_uid,"sid"=>$sid,"eventid"=>$eid))->save($data);
		$this->ajaxReturn($re);
	}
	public function pc_brandme(){
		$shop_uid = I("shop_uid");
		$sid = I("sid");
		$eid = I("eid");
		$EUser = M("EventsUsers");
		$data["refond"]=1;
		$re["status"]=$EUser->where(Array("uzuid"=>$shop_uid,"sid"=>$sid,"eventid"=>$eid))->save($data);
		$this->ajaxReturn($re);
	}
}